<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use Yansongda\Pay\Pay;

class IndexController extends Controller
{
    public function index(){
        $data=Db::name("user")->select();
        $a=file_put_contents(ROOT_PATH."public/log",json_encode($data,JSON_UNESCAPED_UNICODE));
    }
    public function get_test(){
        $data=Db::name("user")->select();
        Db::name("user")->insertAll($data);
    }
    public function test(){
        $fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//获取某一时刻系统cpu和内存使用情况
        $rs = "";
        while(!feof($fp)){
            $rs .= fread($fp,1024);
        }
        pclose($fp);
        $sys_info = explode("\n",$rs);
        $tast_info = explode(",",$sys_info[3]);//进程 数组

        $cpu_info = explode(",",$sys_info[4]); //CPU占有量 数组
        $mem_info = explode(",",$sys_info[5]); //内存占有量 数组
//正在运行的进程数
        $tast_running = trim(trim($tast_info[1],'running'));
//CPU占有量
        $cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比
//内存占有量
        $mem_total = trim(trim($mem_info[0],'Mem: '),'k total');
        $mem_used = trim($mem_info[1],'k used');
        $mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //百分比
        print_r($mem_info);

        /*硬盘使用率 begin*/

        $fp = popen('df -lh | grep -E "^(/)"',"r");
        $rs = fread($fp,1024);
        pclose($fp);
        $rs = preg_replace("/\s{2,}/",' ',$rs); //把多个空格换成 “_”
        $hd = explode(" ",$rs);
        $hd_avail = trim($hd[3],'G'); //磁盘可用空间大小 单位G
        $hd_usage = trim($hd[4],'%'); //挂载点 百分比

        print_r($hd);
        /*硬盘使用率 end*/
//检测时间
        $fp = popen("date +\"%Y-%m-%d %H:%M\"","r");
        $rs = fread($fp,1024);
        pclose($fp);
        $detection_time = trim($rs);
        print_r($detection_time);
    }

    //支付宝   扫码支付
    public function appliy()
    {
        $config=config('appliy');
        $order = [
            'out_trade_no' => date("YmdHis"),
            'subject' => '后台系统-测试',
            'total_amount' => '0.01',//支付宝单位是 元
        ];
        $alipay = Pay::alipay($config)->scan($order);
       var_dump($alipay);
    }

    //微信   扫码支付
    public function weixin(){
        $config=config('wechat');
        $order = [
            'out_trade_no' => date("YmdHis"),
            'body' => '后台系统-测试',
            'total_fee' => '1',   //微信单位是  分
//            'openid' => 'onkVf1FjWS5SBxxxxxxxx',
        ];

        $result = Pay::wechat($config)->scan($order);
        var_dump($result);
    }


}
